/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-23 16:11:24
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-07 21:33:08
 * @FilePath: /SGSim/Export/ExportAPI/ExportAPI.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置:
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "ExportAPI.h"

#include <iostream>

#include <boost/algorithm/string.hpp>

#include "Utility/Configuration/SGConfig.h"
#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"

#include "Export/NH5Export/NH5Export.h"
#include "Export/VTKExport/VTKExport.h"

using namespace SG::Export;
using namespace SG::DataStructure;
using namespace SG::DataStructure::Common;

SGExporter::SGExporter (std::shared_ptr<SG::DBManager::DBServiceFactory> dbServiceFactory)
{
    m_dbServiceFactory = dbServiceFactory;
}

SGExporter::~SGExporter ()
{}

Status SGExporter::Export (const SG::DataStructure::FEM::Project project, const int world_rank)
{
    // 生成输出的文件名称 不带文件后缀
    std::string filepath;

    filepath = project.m_workSpace + project.m_jobName;

    SGInfo << "Begin Export result:" << filepath;

    auto outputSetting = SG::Utility::SGConfig::GetInstance ().GetData ().Has ("Post");
    if (outputSetting)
    {
        auto output = SG::Utility::SGConfig::GetInstance ().GetData ().GetValue ("Post");

        // 输出h5
        if (output.Has ("OutputH5") && output.GetValue ("OutputH5").GetBool ())
        {
            std::string const h5path = filepath + ".h5";

            SGInfo << "OutPut H5 Result:" << h5path;
            SG::Export::NH5Export::WriterNastranHDF5 (m_dbServiceFactory, project, h5path);
        }

        // 输出vtk
        if (output.Has ("OutputVTK") && output.GetValue ("OutputVTK").GetBool ())
        {
            std::string const VTKpath = filepath + ".vtk";

            SGInfo << "OutPut VTK Result:" << VTKpath;
            SG::Post::vtkOutput (m_dbServiceFactory, VTKpath);
        }
    }

    return Status::STATUS_Success;
}
